﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="Grupo.aspx.cs" Inherits=" RzCorportativo.Views.Ferramentas.Grupo" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="LeftContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <%--<script src="../../Recursos/j/libraries/jquery.ui.autocomplete.js" type="text/javascript"></script>
    <script src="../../Recursos/j/eComplete.js" type="text/javascript"></script>--%>
    <script src="<%=this.ResolveUrl("~/Recursos/j/libraries/jquery.ui.autocomplete.js")%>"
        type="text/javascript"></script>
    <script src="<%=this.ResolveUrl("~/Recursos/j/eComplete.js")%>" type="text/javascript"></script>
    <script src="<%=this.ResolveUrl("~/Recursos/j/libraries/jBlockUI.js")%>" type="text/javascript"></script>
    <asp:HiddenField runat="server" ID="GrupoId" />
    <h1>
        Grupo
    </h1>
    <hr />
    <div class="controls controls-row">
        <asp:TextBox runat="server" ID="Descricao" placeholder="Nome" />
        <asp:DropDownList runat="server" ID="Status" Width="265">
            <asp:ListItem Text="Status" Value="-1" />
            <asp:ListItem Text="Ativo" Value="1" />
            <asp:ListItem Text="Inativo" Value="0" />
        </asp:DropDownList>
    </div>
    <div class="controls">
        <div class="container-fluid">
            <%--<div class="row-fluid">
                <div class="span5">
                    <span>Grupo</span>
                    <div class="control-group">
                    </div>
                </div>
            </div>
            <div class="row-fluid">
                <div class="span5">
                    <span>Status</span>
                    <div class="control-group">
                    </div>
                </div>
            </div>--%>
            <div class="row-fluid">
                <div class="control-group">
                    <asp:Button Text="salvar" runat="server" ID="salvar" CssClass="btn btn-success" OnClick="salvar_Click" />
                </div>
            </div>
        </div>
    </div>
    <ul class="nav nav-tabs" id="myTab">
        <li class="active"><a href="#membros">Membros</a></li>
        <li><a href="#itens">Permissões</a></li>
    </ul>
    <div class="tab-content">
        <div class="tab-pane" id="itens">
        </div>
        <div class="tab-pane active" id="membros">
            <div>
                <input placeholder="Login do usuário" type="text" name="complete" value="" auto-complete="true" eid="nome" requesttype="post"
                    contenttype="application/json; charset=utf-8" url="<%=this.ResolveUrl("~/Views/Ferramentas/Grupo.aspx/Listar")%>"
                    isasync="true" length="1" datatype="json" templeted="true" onrender="getItem"
                    onerror="getError" onselected="selectedItem" />
                <button class="btn btn-info" type="button" id="addUser">
                    adicionar</button>
                <input type="hidden" name="LoginId" value="" id="LoginId" />
            </div>
            <div style="background-color: #fafafa; padding: 8px">
                <div id="navbarExample" class="navbar navbar-static" style="margin-bottom: 0px">
                    <div class="navbar-inner">
                        <div class="container" style="width: auto;">
                            <a class="brand" href="#">Usuários</a>
                            <ul class="nav">
                                <li class=""><a href="#mdo"></a></li>
                            </ul>
                        </div>
                    </div>
                </div>
                <div data-spy="scroll" data-target="" data-offset="0" class="scrollspy-example">
                    <div style="overflow: auto; height: 200px; background-color: White">
                        <div style="min-width: 800px; margin-top: 30px" class="grid">
                            <table id="gridUsuarios" class="table-hover">
                                <tbody>
                                    <tr>
                                        <th scope="col">
                                            Login
                                        </th>
                                        <th scope="col">
                                            Usuário
                                        </th>
                                        <th scope="col">
                                            #
                                        </th>
                                    </tr>
                                    <%int i = 0;
                                      foreach (var item in this.Logins)
                                      {%>
                                    <tr id="<%=item.LoginId.ToString()%>" class='<%= i % 2 == 0 ? "gridrow" : "gridrow_alternate"%>'>
                                        <td>
                                            <%=item.LoginText %>
                                        </td>
                                        <td>
                                            <%=item.Usuario.Nome %>
                                        </td>
                                        <td>
                                            <button type='button' class='btn btn-danger' onclick="Remove(<%=this.GrupoId.Value%>, <%=item.LoginId.ToString()%>)">
                                                Remover</button>
                                        </td>
                                    </tr>
                                    <%i += 1;
                                      } %>
                                </tbody>
                            </table>
                            <br />
                            <br />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div modal="usuarios">
        <div modal-content="usuarios">
        </div>
    </div>
    <script type="text/javascript">
        function getItem(ul, item) {

            var inner_html = '<a><div class="list_item_container"><div class=""><strong>Login:</strong> ' + item.label + '</div><div class="description"><strong>Nome: </strong>' + item.description + '</div></div></a>';
            return $("<li style='width:350px'></li>")
            .data("item.autocomplete", item)
            .append(inner_html)
            .appendTo(ul);
        }
        function getError(e) {
            var _alert = new Alert();
            _alert.Name = "erroGeral";
            _alert.Type = AlertType.Error;
            _alert.AddMessage(e.d ? e.d : e.responseText);
            _alert.Show();
        }

        function sourceError(e) {
            var _alert = new Alert();
            _alert.Name = "erroGeral";
            _alert.Type = AlertType.Error;
            _alert.AddMessage(e.d ? e.d : e.responseText);
            _alert.Show();

        }
        function selectedItem(event, ui, context) {
            var item = ui.item && ui.item.value ? ui.item : context;
            if (item.loginId) {
                $("#LoginId").val(item.loginId.toString())
            }
        }
        $(function () {
            $('#myTab membros').tab('show');
            $('#myTab a').click(function (e) {
                e.preventDefault();
                $(this).tab('show');
            })
            $("#addUser").click(function (e) {
                var g = new Grupo();
                g.GrupoId = $("input[id$='GrupoId']").val();
                g.AddUser($("#LoginId").val());
                return;
                $('div[id="membros"]').block({ message: 'aguarde <img src="' + eS.UrlPadrao + 'recursos/i/loading.gif" />' });
                $.ajax({
                    url: '<%=this.ResolveUrl("~/Views/Ferramentas/Grupo.aspx/Adicionar")%>',
                    type: 'post',
                    data: '{ loginId: ' + $("#LoginId").val() + ', grupoId: ' + $("input[id$='GrupoId']").val() + ' }',
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    success: function (e) {
                        if (e.d) {
                            var classe = $('#gridUsuarios tr:last').attr("class");
                            if (!classe) {
                                classe = "gridrow";
                            } else {
                                classe = classe == "gridrow" ? "gridrow_alternate" : "gridrow";
                            }
                            var tr = "<tr class='" + classe + "'><td>" + e.d.LoginText + "</td><td>" + e.d.Usuario.Nome + "</td><td><button type='button' class='btn btn-danger'>Remover</button></td></tr>";
                            $('#gridUsuarios tr:last').after(tr);
                            $("input[eId='nome']").val("");
                            $("#LoginId").val("");
                        }
                    },
                    error: function (e) {
                        var _alert = new Alert();
                        _alert.Name = "erroGeral";
                        _alert.Type = AlertType.Error;
                        _alert.AddMessage(e.d ? e.d : e.responseText);
                        _alert.Show();
                    },
                    complete: function (e) {
                        $('div[id="membros"]').unblock();
                    }
                });
            });
        })
        function Grupo() {
            var __grupo = this;
            this.GrupoId = "0";
            this.Url = '<%=this.ResolveUrl("~/Views/Ferramentas/Grupo.aspx/")%>';
            __grupo.GetObject = function (loginId) {
                var item = '{ loginId: ' + loginId.toString() + ', grupoId: ' + this.GrupoId.toString() + ' }';
                return item;
            }
            this.AddUser = function (loginId) {
                ExecuteMethod(this.Url + "Adicionar", __grupo.GetObject(loginId), function (e) {
                    if (e.d) {
                        var classe = $('#gridUsuarios tr:last').attr("class");
                        if (!classe) {
                            classe = "gridrow";
                        } else {
                            classe = classe == "gridrow" ? "gridrow_alternate" : "gridrow";
                        }
                        var tr = "<tr id='" + e.d.LoginId.toString() + "' class='" + classe + "'><td>" + e.d.LoginText + "</td><td>" + e.d.Usuario.Nome + "</td><td><button type='button' class='btn btn-danger' onclick='Remove(" + __grupo.GrupoId.toString() + ", " + e.d.LoginId.toString() + ")'>Remover</button></td></tr>";
                        $('#gridUsuarios tr:last').after(tr);
                        $("input[eId='nome']").val("");
                        $("#LoginId").val("");
                    }
                });
            }
            this.RemoveUser = function (loginId) {
                ExecuteMethod(__grupo.Url + "Remover", __grupo.GetObject(loginId), function (e) {
                    if (e.d) {
                        $("tr[id='" + e.d.LoginId.toString() + "']").remove();
                    }
                });
            }
            return this;
        }

        function Remove(grupoId, loginId) {
            var grupo = new Grupo();
            grupo.GrupoId = grupoId;
            grupo.RemoveUser(loginId);
        }

        function ExecuteMethod(url, parans, callback) {
            $('div[id="membros"]').block({ message: 'aguarde <img src="' + eS.UrlPadrao + '/recursos/i/loading.gif" />' });
            $.ajax({
                url: url,
                type: 'post',
                data: parans,
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: callback,
                error: function (e) {
                    var _alert = new Alert();
                    _alert.Name = "erroGeral";
                    _alert.Type = AlertType.Error;
                    _alert.AddMessage(e.d ? e.d : e.responseText);
                    _alert.Show();
                },
                complete: function (e) {
                    $('div[id="membros"]').unblock();
                }
            });
        }
    </script>
</asp:Content>
